/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pope.jloading;

import pope.jloading.downloader.Config;
import pope.jloading.downloader.Downloader;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import pope.jloading.TaskManager;

public class TaskModel {
    //private Config config;  // 状态保存文件
    
    public final static String[] TITLES = {
        "状态", "文件",  "进度"
    };
    
    public final static int COLUMN_STATE = 0;   // 状态
    public final static int COLUMN_NAME = 1;    // 文件名
//    public final static int COLUMN_SIZE = 2;    // 文件大小(字节)
    public final static int COLUMN_PROCESS =2; // 进度
//    public final static int COLUMN_SPEED = 4;   // 速度
//    public final static int COLUMN_LOADED = 5;  // 已经下载
//    public final static int COLUMN_LEAVE = 6;   // 剩余时间
//    public final static int COLUMN_TYPE = 7;    // 文件类型
//    public final static int COLUMN_SAVE = 8;    // 文件下载路径
    
    // 字段(显示）
    //private String state;
    private String name;
    private long size;
    private long process;
    private long speed;
    private long loaded;
    private String leave;
    private String type;
    private String save;    // 保存路径
    
    private String taskid;  // 任务标识
    private String url;     // 下载源地址(非显示)
    private String sid;
    private String action;
    
    public TaskModel(Config config) {
        taskid = config.getId();
        name = config.getName();
        url = config.getURLs().get(0);
        save = config.getSave();
        size = config.getLength();
        type = url.substring(url.lastIndexOf(".") + 1);
        sid=config.getSid();
        init();
    }
    
    public TaskModel(String taskid, String name, String url, String save) {
        this.taskid = taskid;
        this.name = name;
        this.url = url;
        this.save = save;
        type = url.substring(url.lastIndexOf(".") + 1);
        init();
    }
    /**
     * edit by pope
     * @param taskid
     * @param name
     * @param url
     * @param save
     * @param sid
     */
    public TaskModel(String taskid, String name, String url, String save,String sid,String action) {
        this.taskid = taskid;
        this.name = name;
        this.url = url;
        this.save = save;
        this.sid=sid;
        this.action=action;
        type = url.substring(url.lastIndexOf(".") + 1);
        init();
    }
    
    private void init() {
        if (downloader == null) {
            TaskManager tm = TaskManager.getInstance();
            Config config = tm.getConfig(taskid);// 这里是怎么得到Config的
            System.out.println("taskid "+taskid);
            if (config != null) {
                System.out.println("config存在");
                downloader = new Downloader(config);// config
            } else {
                System.out.println("config不存在");
                downloader = new Downloader(taskid, name, url, save, 12,sid,action);
            }
        }
    }
    
    // -------------------------------------------------- 121214e3243543654353
    
    public Object getColumnValue(int columnIndex) {
        switch (columnIndex) {
            case COLUMN_STATE:{
                return getState();
            }
            case COLUMN_NAME:{ 
                return name;
            }
            /*case COLUMN_SIZE:{
                if (downloader != null) {
                    size = downloader.getTotalBytes();
                }
                return size;
            }*/
            case COLUMN_PROCESS:{
            	size = downloader.getTotalBytes();
                loaded = downloader.getReadBytes();//这是补充的
                if (size == 0) return 0;               
                process = loaded * 100 / size;
                return (int) process;
            }
           /* case COLUMN_SPEED:{
                if (downloader != null) {
                    speed = downloader.getSpeed();
                } else {
                    speed = 0;
                }
                return speed + " k/s";
            }
            case COLUMN_LOADED:{
                if (downloader != null) {
                    loaded = downloader.getReadBytes();
                }
                return loaded + " bytes";
            }
            case COLUMN_LEAVE:{
                leave = getLeaveTime();
                return leave;
            }
           case COLUMN_TYPE:{ 
                return type;
            }
            case COLUMN_SAVE:{ 
                return save == null ? "": save;
            }*/
        }
        return null;
    }
    
    Calendar cal = Calendar.getInstance();
    SimpleDateFormat stf = new SimpleDateFormat("mm:ss");
    public String getLeaveTime() {
        if (speed <= 0) return "";
        long leaveTime = (size - loaded) / speed;
        long temp = (leaveTime / 1000 / 60 / 60);
        String hh;
        if (temp < 10) {
            hh = "0" + temp;
        } else {
            hh = String.valueOf(temp);
        }
        cal.setTimeInMillis(leaveTime);
        return (hh + ":" + stf.format(cal.getTime()));
    }

    // -------------------------------------------------- downloader
    
    private Downloader downloader;
    
    /** 开始任务下载 */
    public void toStart() {
        if (downloader == null) init();
        downloader.toStart();
    }
    /**
     * 下载某个序列号 sid
     * @param sid
     */
    public void toStart(String sid) {
        if (downloader == null) init();
        downloader.toStart();
    }
    
    /** 暂停任务 */
    public void toPause() {
        if (downloader != null) {
            downloader.toPause();
        }
    }
    
    /** 停止任务下载 */
    public void toStop() {
        if (downloader != null) {
            downloader.toStop();
            downloader = null;
        }
    }
    
    /** 删除任务 */
    public boolean toDelete() {
        if (downloader != null) {
            return downloader.toDelete();
        }
        return false;
    }
    
    // -------------------------------------------------- 
    
    public String getState() {
        if (downloader == null) return "初始化";
        if (process==100) {
            return "完成";
        } else  if (downloader.isNone()) {
            return "初始化";
        } else if (downloader.isReady()) {
            return "准备";
        } else if (downloader.isLoading()) {
            return "下载中";
        } else if (downloader.isPaused()) {
            return "暂停";
        } else if (downloader.isStopped()) {
            return "停止";
        }
        return "";
    }
    
    /** 获取任务标识 */
    public String getTaskid() {
        return taskid;
    }
}

